#include "exporter/KMLExporter.h"

/**
 * @brief Dictionnary that store writter to KML files:
 *
 */
std::map<std::string, std::ofstream> all_kml_files;

/**
 * @brief Open and write KML header in file
 *
 * @param file_path
 */
void openKMLFile(std::string file_path) {
    if (all_kml_files.count(file_path) > 0)
        return;
    all_kml_files[file_path].open(file_path);
    all_kml_files[file_path] << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
    all_kml_files[file_path] << "<kml xmlns=\"http://www.opengis.net/kml/2.2\">" << std::endl;
    all_kml_files[file_path] << "\t<Document>" << std::endl;
    all_kml_files[file_path] << "\t\t<name>generated_path</name>" << std::endl;
    all_kml_files[file_path] << "\t\t<description>Paths generated by ZED SDK</description>" << std::endl;
    all_kml_files[file_path] << "<Style id=\"my-awesome-light-blue\">\n";
    all_kml_files[file_path] << "\t<LineStyle>\n";
    all_kml_files[file_path] << "\t\t<color>57F2DA</color>\n";
    all_kml_files[file_path] << "\t\t<width>4</width>\n";
    all_kml_files[file_path] << "\t</LineStyle>\n";
    all_kml_files[file_path] << "\t<PolyStyle>\n";
    all_kml_files[file_path] << "\t\t<color>57F2DA</color>\n";
    all_kml_files[file_path] << "\t</PolyStyle>\n";
    all_kml_files[file_path] << "</Style>\n";
    all_kml_files[file_path] << "<Placemark>\n";
    all_kml_files[file_path] << "\t<name>generated_path</name>\n";
    all_kml_files[file_path] << "\t<description>generated by zed SDK</description>\n";
    all_kml_files[file_path] << "\t<styleUrl>#57F2DA</styleUrl>\n";
    all_kml_files[file_path] << "\t<LineString>\n";
    all_kml_files[file_path] << "\t\t<extrude>1</extrude>\n";
    all_kml_files[file_path] << "\t\t<tessellate>1</tessellate>\n";
    all_kml_files[file_path] << "\t\t<altitudeMode>absolute</altitudeMode>\n";
    all_kml_files[file_path] << "\t<coordinates> ";
}

/**
 * @brief Close all KML file writer and place KML files footer
 *
 */
void closeAllKMLWriter() {

    for (auto& it : all_kml_files) {
        it.second << "\t</coordinates>\n";
        it.second << "\t</LineString>\n";
        it.second << "</Placemark>\n";
        it.second << "\t</Document>" << std::endl;
        it.second << "</kml>" << std::endl;
        it.second.close();
    }
}

/**
 * @brief Save GeoPose in KML file that can be displayed into google map (maps.google.com)
 *
 * @param file_path path expected for the resulted KML file
 * @param geopose current data to save
 */
void saveKMLData(std::string file_path, sl::GeoPose geopose) {
    if (all_kml_files.count(file_path) == 0)
        openKMLFile(file_path);
    double latitude, longitude, altitude;
    geopose.latlng_coordinates.getCoordinates(latitude, longitude, altitude, false);
    all_kml_files[file_path] << std::to_string(longitude) + ", " + std::to_string(latitude) + ", " + std::to_string(altitude) + "\n";
}
/**
 * @brief Save GNSSData in KML file that can be displayed into google map (maps.google.com)
 *
 * @param file_path path expected for the resulted KML file
 * @param gnss_data current data to save
 */
void saveKMLData(std::string file_path, sl::GNSSData gnss_data) {
    if (all_kml_files.count(file_path) == 0)
        openKMLFile(file_path);
    double latitude, longitude, altitude;
    gnss_data.getCoordinates(latitude, longitude, altitude, false);
    all_kml_files[file_path] << std::to_string(longitude) + ", " + std::to_string(latitude) + ", " + std::to_string(altitude) + "\n";
}
